package com.cyou.gamehistory.service;


import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.cyou.framework.pagination.proxy.PaginationSupport;
import com.cyou.framework.util.StringUtility;
import com.cyou.gamehistory.entity.UserInfo;
import com.cyou.gamehistory.repository.UserInfoDao;

/**
 * UserInfoService
 * 
 * @author wangzhang date 2012-12-14 下午7:35:54
 */
@Component
@Transactional(readOnly = true)
public class UserInfoService {

	@Autowired
	private UserInfoDao userInfoDao;

	public UserInfo getUserInfoByUserId(Integer userId) {
		return userInfoDao.getByUserId(userId);
	}
	@Transactional
	@SuppressWarnings("deprecation")
	public void updateUserInfo(UserInfo user,Double totalCost,Integer beginYear,Integer gameCount,String honor,Integer storyCount) {
		
		user.setCostCount(totalCost);
		user.setGameBeginTime(beginYear);
		user.setGameCount(gameCount);
		user.setGameLastestTime(new Date().getYear()+1900);
		user.setUpdateCount(user.getUpdateCount()+1);
		user.setHonor(honor);
		//user.setGameHistoryImage(String.valueOf(user.getUserId())+".jpg");
		//user.setRank(userInfoService.calculateRank(orderNum.length));
		user.setStoryCount(storyCount);
		userInfoDao.update(user);
	}

	/**
	 * 根据游戏数量获取游戏排名
	 * 
	 * @param gameCount
	 * @return
	 */
	public Integer calculateRank(Integer userId) {
		return userInfoDao.rank(userId);
	}

	/**
	 * 总人数
	 * 
	 * @return
	 */
	public Integer userCount() {
		return userInfoDao.userCount();
	}

	/**
	 * 前50达人排行 （如果新用户，则忽略）
	 * 
	 * @return
	 */
	public List<UserInfo> getTopUserInfoList() {
		List<UserInfo> list = userInfoDao.getTopUserInfoList();
		Iterator<UserInfo> i = list.iterator();
		while (i.hasNext()) {
			UserInfo userinfo = i.next();
			if (userinfo.getGameCount() == 0) {
				i.remove();
			}
		}
		return list;
	}

	/**
	 * 获取用户分页数据
	 * 
	 * @param orderFlag
	 *            时间升序或者降序标记
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public PaginationSupport<UserInfo> getUserInfoList(String nickname,String orderFlag,String createTimeBegin,String createTimeEnd,
			Integer pageSize, Integer pageNo) {
		Map<String, Object> map = new HashMap<String, Object>();

		PaginationSupport<UserInfo> pageinationSupport = new PaginationSupport<UserInfo>();
		pageinationSupport.setCurrentPage(pageNo);
		pageinationSupport.setPageSize(pageSize);

			
		if(StringUtility.isNotBlank(createTimeBegin)){
			createTimeBegin=createTimeBegin+" 00:00:00";
		}
		if(StringUtility.isNotBlank(createTimeEnd)){
			createTimeEnd=createTimeEnd+" 23:59:59";
		}
		map.put("page", pageinationSupport);
		map.put("orderFlag", orderFlag);
		map.put("createTimeBegin", createTimeBegin);
		map.put("createTimeEnd", createTimeEnd);
		map.put("nickname", nickname);
		PaginationSupport<UserInfo> userInfoList = userInfoDao.findAllUserInfoList(map);
		return userInfoList;

	}
	@Transactional
	public void saveUserInfo(UserInfo userInfo){
		userInfoDao.save(userInfo);
	}
}
